package cn.iocoder.yudao.module.member.service.praxisProjectType;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.number.NumberUtils;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.member.controller.admin.praxisProjectType.vo.PraxisProjectTypeCreateReqVO;
import cn.iocoder.yudao.module.member.controller.admin.praxisProjectType.vo.PraxisProjectTypeExportReqVO;
import cn.iocoder.yudao.module.member.controller.admin.praxisProjectType.vo.PraxisProjectTypePageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.praxisProjectType.vo.PraxisProjectTypeUpdateReqVO;
import cn.iocoder.yudao.module.member.convert.praxisProjectType.PraxisProjectTypeConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.praxisProjectType.PraxisProjectTypeDO;
import cn.iocoder.yudao.module.member.dal.mysql.praxisProjectType.PraxisProjectTypeMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.PRAXIS_PROJECT_TYPE_NOT_EXISTS;

/**
 * 实践项目类型 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class PraxisProjectTypeServiceImpl implements PraxisProjectTypeService {

    @Resource
    private PraxisProjectTypeMapper praxisProjectTypeMapper;

    @Override
    public Long createPraxisProjectType(PraxisProjectTypeCreateReqVO createReqVO) {
        // 插入
        PraxisProjectTypeDO praxisProjectType = PraxisProjectTypeConvert.INSTANCE.convert(createReqVO);
        String loginUserName = WebFrameworkUtils.getLoginUserName();
        praxisProjectType.setCreator(loginUserName);
        praxisProjectType.setCreateTime(new Date());
        praxisProjectType.setUpdater(loginUserName);
        praxisProjectType.setUpdateTime(new Date());
        Integer maxWeight = NumberUtils.getInteger(praxisProjectTypeMapper.getMaxWeight());
        praxisProjectType.setWeight(maxWeight+1);
        praxisProjectTypeMapper.insert(praxisProjectType);
        // 返回
        return praxisProjectType.getId();
    }

    @Override
    public void updatePraxisProjectType(PraxisProjectTypeUpdateReqVO updateReqVO) {
        // 校验存在
        this.validatePraxisProjectTypeExists(updateReqVO.getId());
        // 更新
        PraxisProjectTypeDO updateObj = PraxisProjectTypeConvert.INSTANCE.convert(updateReqVO);
        updateObj.setUpdater( WebFrameworkUtils.getLoginUserName());
        updateObj.setUpdateTime(new Date());
        praxisProjectTypeMapper.updateById(updateObj);
    }

    @Override
    public void deletePraxisProjectType(Long id) {
        // 校验存在
        this.validatePraxisProjectTypeExists(id);
        // 删除
        praxisProjectTypeMapper.deleteById(id);
    }

    private void validatePraxisProjectTypeExists(Long id) {
        if (praxisProjectTypeMapper.selectById(id) == null) {
            throw exception(PRAXIS_PROJECT_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public PraxisProjectTypeDO getPraxisProjectType(Long id) {
        return praxisProjectTypeMapper.selectById(id);
    }

    @Override
    public List<PraxisProjectTypeDO> getPraxisProjectTypeList(Collection<Long> ids) {
        return praxisProjectTypeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PraxisProjectTypeDO> getPraxisProjectTypePage(PraxisProjectTypePageReqVO pageReqVO) {
        return praxisProjectTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PraxisProjectTypeDO> getPraxisProjectTypeList(PraxisProjectTypeExportReqVO exportReqVO) {
        return praxisProjectTypeMapper.selectList(exportReqVO);
    }

    @Override
    public void updateProjectTypeSort(List<Long> ids) {
        List<PraxisProjectTypeDO> list=new ArrayList<>();
        int size = ids.size();
        for (int i = 0; i <size ; i++) {
            PraxisProjectTypeDO r=new PraxisProjectTypeDO();
            r.setId(ids.get(i));
            r.setWeight(size-i);
            list.add(r);
        }
        praxisProjectTypeMapper.updateProjectTypeSort(list);
    }
}
